ALTER       proc sp_create_update_view_cost_by_location (

@process_step_id bigint,
@location_Id INT,
@Q1_Standard_Cost float,
@Q1_Yield float,
@Q2_Standard_Cost float,
@Q2_Yield float,
@Q3_Standard_Cost float,
@Q3_Yield float,
@Q4_Standard_Cost float,
@Q4_Yield float,
@Projected_Cost float,
@Projected_Yield float,	
@dateYear int,
@cost_Type VARCHAR(10)
)
as
begin

	declare @temp_process_step_id int
	Select @temp_process_step_id = ProcessStep_Id from update_view_cost_table where ProcessStep_ID = @process_step_id and Location_ID = @location_Id and Year(Fin_Year) = @dateYear
	
	if @temp_process_step_id is null
	begin 
		INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Location_Id, Q1_Standard_Cost, Q1_Yield, Q2_Standard_Cost, Q2_Yield, Q3_Standard_Cost, Q3_Yield, Q4_Standard_Cost, Q4_Yield, Fin_Year, Timestamp, Projected_Cost, Projected_Yield)
		VALUES(@process_step_id, @location_Id, @Q1_Standard_Cost, @Q1_Yield, @Q2_Standard_Cost, @Q2_Yield, @Q3_Standard_Cost, @Q3_Yield, @Q4_Standard_Cost, @Q4_Yield, '01/01/' + CONVERT(VARCHAR,@dateYear), CURRENT_TIMESTAMP,@Projected_Cost, @Projected_Yield)
	end
	else
	begin
		if @cost_Type = 'Standard'
		BEGIN
			UPDATE Update_View_Cost_Table set 
			Q1_Standard_Cost = @Q1_Standard_Cost, Q1_Yield = @Q1_Yield, 
			Q2_Standard_Cost = @Q2_Standard_Cost, Q2_Yield = @Q2_Yield, 
			Q3_Standard_Cost = @Q3_Standard_Cost, Q3_Yield = @Q3_Yield, 
			Q4_Standard_Cost = @Q4_Standard_Cost, Q4_Yield = @Q4_Yield,
			Timestamp = CURRENT_TIMESTAMP where ProcessStep_id = @process_step_id and Location_ID = @location_Id and year(Fin_Year) = @dateYear
		END
		ELSE
		BEGIN
			UPDATE Update_View_Cost_Table set Projected_Cost = @Projected_Cost, Projected_Yield = @Projected_Yield,
			Timestamp = CURRENT_TIMESTAMP where ProcessStep_id = @process_step_id and Location_ID = @location_Id and year(Fin_Year) = @dateYear
		END
	end

end
